package com.company.project.service.impl;

import com.alibaba.fastjson.JSON;
import com.company.project.common.exception.BusinessException;
import com.company.project.common.utils.DateUtils;
import com.company.project.entity.SysDept;
import com.company.project.entity.SysUser;
import com.company.project.service.*;
import com.company.project.vo.req.MytraRequestVO;
import com.company.project.vo.resp.MytraListVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.company.project.mapper.BusiMytraMapper;
import com.company.project.entity.BusiMytraEntity;

import javax.annotation.Resource;
import java.util.*;


@Service("busiMytraService")
public class BusiMytraServiceImpl extends ServiceImpl<BusiMytraMapper, BusiMytraEntity> implements BusiMytraService {

    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private UserService userService;
    @Resource
    private DeptService deptService;
    @Resource
    private RedisService redisService;

    @Value("${spring.redis.key.prefix.userPos}")
    private String posPrefix;

    @Value("${spring.redis.key.expire.userToken}")
    private int exire;

    @Resource
    private BusiMytraMapper busiMytraMapper;

    @Override
    public void wholeAdd(BusiMytraEntity busiMytraEntity) throws BusinessException {
        // 查询当前用户ID
        String userId= httpSessionService.getCurrentUserId();
        busiMytraEntity.setUserId(userId);
        // 查询当前用户所属部门名称
        SysUser user = userService.getById(userId);
        String deptId = user.getDeptId();
        busiMytraEntity.setName(user.getRealName());
        SysDept sysDept = deptService.getById(deptId);
        String detpName = sysDept.getName();
        busiMytraEntity.setDetp(detpName);
        String now = DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN);
        busiMytraEntity.setCreateTime(now);
        busiMytraEntity.setUpdateTime(now);

        // 存储人员位置信息到redis
        String pos = JSON.toJSONString(busiMytraEntity);
        String key = posPrefix + userId;
        redisService.setAndExpire(key, pos, exire);

        this.save(busiMytraEntity);
    }

    @Override
    public List<BusiMytraEntity> wholeGetPos(String userId) throws BusinessException {

        final List<BusiMytraEntity> posList = new ArrayList<>();

        String pos = null;
        if (StringUtils.isBlank(userId)) {
            Set<String> keys = redisService.keys(posPrefix, true);
            keys.forEach(e -> {
                String p = redisService.get(e);
                if (StringUtils.isNotBlank(p)) {
                    posList.add(JSON.parseObject(p, BusiMytraEntity.class));
                }
            });
        } else {
            pos = redisService.get(posPrefix + userId);
            if (StringUtils.isNotBlank(pos)) {
                posList.add(JSON.parseObject(pos, BusiMytraEntity.class));
            }
        }
        return posList;
    }

    @Override
    public List<MytraListVO> wholeGetMytralist(MytraRequestVO mytraRequestVO) throws BusinessException {
        return busiMytraMapper.wholeGetMytralist(mytraRequestVO);
    }

}